<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.16"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>LibTools: H:/project/LibTools/src/algorithm/SafeSet.h 源文件</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">LibTools
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.8.16 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',false,false,'search.php','搜索');
});
/* @license-end */</script>
<div id="main-nav"></div>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_68267d1309a1af8e8297ef4c3efbcdba.html">src</a></li><li class="navelem"><a class="el" href="dir_fbc178c12bd249518b3c0868875b8083.html">algorithm</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">SafeSet.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="_safe_set_8h.html">浏览该文件的文档.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#ifndef __SAFESET_H__</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#define __SAFESET_H__</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="preprocessor">#include &quot;../thread/Mutex.h&quot;</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="preprocessor">#include &lt;set&gt;</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160; </div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<a class="code" href="tools_8h.html#a201ce692c9dca6599e5c12d00167419b">DEF_BEG_LIBTOOLS</a></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160; </div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="keyword">template</span>&lt;<span class="keyword">class</span> Kt, <span class="keyword">class</span> Ft = std::less&lt;Kt&gt; &gt;</div>
<div class="line"><a name="l00012"></a><span class="lineno"><a class="line" href="class_c_safe_set.html">   12</a></span>&#160;<span class="keyword">class </span><a class="code" href="class_c_safe_set.html">CSafeSet</a></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;{</div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00016"></a><span class="lineno"><a class="line" href="class_c_safe_set.html#a5eac39761f068db3cb3de5adbb17d1bf">   16</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_c_safe_set.html#a5eac39761f068db3cb3de5adbb17d1bf">Insert</a>(<span class="keyword">const</span> Kt&amp; key)</div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;    {</div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;        <a class="code" href="class_c_lock_gurad.html">CLockGurad</a> lock(&amp;<a class="code" href="class_c_safe_set.html#ab83a25871d5b7804164067d2621753c3">_mutexSet</a>);</div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;         <a class="code" href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">_setData</a>.insert(key);</div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;    }</div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160; </div>
<div class="line"><a name="l00027"></a><span class="lineno"><a class="line" href="class_c_safe_set.html#a186f6ee5dbc0837992b68810d933494c">   27</a></span>&#160;    <span class="keywordtype">bool</span> <a class="code" href="class_c_safe_set.html#a186f6ee5dbc0837992b68810d933494c">Erase</a>(<span class="keyword">const</span> Kt&amp; key)</div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;    {</div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;        <a class="code" href="class_c_lock_gurad.html">CLockGurad</a> lock(&amp;<a class="code" href="class_c_safe_set.html#ab83a25871d5b7804164067d2621753c3">_mutexSet</a>);</div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;        <span class="keyword">typename</span> std::set&lt;Kt, Ft&gt;::iterator it = <a class="code" href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">_setData</a>.find(key);</div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;        <span class="keywordflow">if</span> (it != <a class="code" href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">_setData</a>.end())</div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;        {</div>
<div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;            <a class="code" href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">_setData</a>.erase(key);</div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;            <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;        }</div>
<div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160; </div>
<div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;        <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;    }</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160; </div>
<div class="line"><a name="l00041"></a><span class="lineno"><a class="line" href="class_c_safe_set.html#ae4a6e1f60961a16e39e5742e0feb3306">   41</a></span>&#160;    <span class="keywordtype">bool</span> <a class="code" href="class_c_safe_set.html#ae4a6e1f60961a16e39e5742e0feb3306">Find</a>(<span class="keyword">const</span> Kt&amp; key)</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;    {</div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;        <a class="code" href="class_c_lock_gurad.html">CLockGurad</a> lock(&amp;<a class="code" href="class_c_safe_set.html#ab83a25871d5b7804164067d2621753c3">_mutexSet</a>);</div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;        <span class="keywordflow">if</span> (<a class="code" href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">_setData</a>.find(key) == <a class="code" href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">_setData</a>.end()) <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160; </div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;        <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;    }</div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160; </div>
<div class="line"><a name="l00050"></a><span class="lineno"><a class="line" href="class_c_safe_set.html#a7af52d74336ec0f71efed1155757dd17">   50</a></span>&#160;    std::size_t <a class="code" href="class_c_safe_set.html#a7af52d74336ec0f71efed1155757dd17">Size</a>()</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;    {</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;        <a class="code" href="class_c_lock_gurad.html">CLockGurad</a> lock(&amp;<a class="code" href="class_c_safe_set.html#ab83a25871d5b7804164067d2621753c3">_mutexSet</a>);</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;        <span class="keywordflow">return</span> <a class="code" href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">_setData</a>.size();</div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;    }</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00056"></a><span class="lineno"><a class="line" href="class_c_safe_set.html#ab83a25871d5b7804164067d2621753c3">   56</a></span>&#160;    <a class="code" href="class_c_mutex.html">CMutex</a> <a class="code" href="class_c_safe_set.html#ab83a25871d5b7804164067d2621753c3">_mutexSet</a>;</div>
<div class="line"><a name="l00057"></a><span class="lineno"><a class="line" href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">   57</a></span>&#160;    std::set&lt;Kt, Ft&gt; <a class="code" href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">_setData</a>;</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;};</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160; </div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;<a class="code" href="tools_8h.html#ac909db71799bb22cb7d636a34cd785e4">DEF_END_LIBTOOLS</a></div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160; </div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;<span class="preprocessor">#endif // __SAFESET_H__</span></div>
</div><!-- fragment --></div><!-- contents -->
<div class="ttc" id="aclass_c_safe_set_html_a186f6ee5dbc0837992b68810d933494c"><div class="ttname"><a href="class_c_safe_set.html#a186f6ee5dbc0837992b68810d933494c">CSafeSet::Erase</a></div><div class="ttdeci">bool Erase(const Kt &amp;key)</div><div class="ttdoc">查找并返回结果值，之后在清除该元素</div><div class="ttdef"><b>Definition:</b> <a href="_safe_set_8h_source.html#l00027">SafeSet.h:27</a></div></div>
<div class="ttc" id="aclass_c_safe_set_html_ae4a6e1f60961a16e39e5742e0feb3306"><div class="ttname"><a href="class_c_safe_set.html#ae4a6e1f60961a16e39e5742e0feb3306">CSafeSet::Find</a></div><div class="ttdeci">bool Find(const Kt &amp;key)</div><div class="ttdoc">判断键是否存在集合中</div><div class="ttdef"><b>Definition:</b> <a href="_safe_set_8h_source.html#l00041">SafeSet.h:41</a></div></div>
<div class="ttc" id="aclass_c_safe_set_html_a7af52d74336ec0f71efed1155757dd17"><div class="ttname"><a href="class_c_safe_set.html#a7af52d74336ec0f71efed1155757dd17">CSafeSet::Size</a></div><div class="ttdeci">std::size_t Size()</div><div class="ttdoc">返回当前大小</div><div class="ttdef"><b>Definition:</b> <a href="_safe_set_8h_source.html#l00050">SafeSet.h:50</a></div></div>
<div class="ttc" id="aclass_c_mutex_html"><div class="ttname"><a href="class_c_mutex.html">CMutex</a></div><div class="ttdoc">常规锁</div><div class="ttdef"><b>Definition:</b> <a href="_mutex_8h_source.html#l00025">Mutex.h:25</a></div></div>
<div class="ttc" id="aclass_c_safe_set_html_afa17db95edba22e1378b27adca9ac103"><div class="ttname"><a href="class_c_safe_set.html#afa17db95edba22e1378b27adca9ac103">CSafeSet::_setData</a></div><div class="ttdeci">std::set&lt; Kt, Ft &gt; _setData</div><div class="ttdef"><b>Definition:</b> <a href="_safe_set_8h_source.html#l00057">SafeSet.h:57</a></div></div>
<div class="ttc" id="aclass_c_safe_set_html"><div class="ttname"><a href="class_c_safe_set.html">CSafeSet</a></div><div class="ttdoc">线程安全的集合类</div><div class="ttdef"><b>Definition:</b> <a href="_safe_set_8h_source.html#l00012">SafeSet.h:12</a></div></div>
<div class="ttc" id="atools_8h_html_a201ce692c9dca6599e5c12d00167419b"><div class="ttname"><a href="tools_8h.html#a201ce692c9dca6599e5c12d00167419b">DEF_BEG_LIBTOOLS</a></div><div class="ttdeci">#define DEF_BEG_LIBTOOLS</div><div class="ttdef"><b>Definition:</b> <a href="tools_8h_source.html#l00036">tools.h:36</a></div></div>
<div class="ttc" id="aclass_c_safe_set_html_ab83a25871d5b7804164067d2621753c3"><div class="ttname"><a href="class_c_safe_set.html#ab83a25871d5b7804164067d2621753c3">CSafeSet::_mutexSet</a></div><div class="ttdeci">CMutex _mutexSet</div><div class="ttdef"><b>Definition:</b> <a href="_safe_set_8h_source.html#l00056">SafeSet.h:56</a></div></div>
<div class="ttc" id="aclass_c_lock_gurad_html"><div class="ttname"><a href="class_c_lock_gurad.html">CLockGurad</a></div><div class="ttdoc">常规锁包装器</div><div class="ttdef"><b>Definition:</b> <a href="_mutex_8h_source.html#l00041">Mutex.h:41</a></div></div>
<div class="ttc" id="atools_8h_html_ac909db71799bb22cb7d636a34cd785e4"><div class="ttname"><a href="tools_8h.html#ac909db71799bb22cb7d636a34cd785e4">DEF_END_LIBTOOLS</a></div><div class="ttdeci">#define DEF_END_LIBTOOLS</div><div class="ttdef"><b>Definition:</b> <a href="tools_8h_source.html#l00038">tools.h:38</a></div></div>
<div class="ttc" id="aclass_c_safe_set_html_a5eac39761f068db3cb3de5adbb17d1bf"><div class="ttname"><a href="class_c_safe_set.html#a5eac39761f068db3cb3de5adbb17d1bf">CSafeSet::Insert</a></div><div class="ttdeci">void Insert(const Kt &amp;key)</div><div class="ttdoc">插入一个元素</div><div class="ttdef"><b>Definition:</b> <a href="_safe_set_8h_source.html#l00016">SafeSet.h:16</a></div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
制作者 &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.16
</small></address>
</body>
</html>
